<?php
$isReloadable = true;
require_once("../../pers/inc.config.php");
require_once(R2_FS_WEB_ROOT.'/app/_inc/inc.session.php');
require_once(R2_FS_WEB_ROOT.'/app/_inc/funct.html.php');
//dbg_($aStatiRint);
$aFlagVal = array('X'=>'da non valutare','N'=>'da valutare','S'=>'valutata','C'=>'');
$aFlagEva = array('X'=>'da non evadere','N'=>'da evadere','S'=>'evasa','C'=>'');

//setta default var. sess.
$aDefVarSess = array();
$aDefVarSess['f_tes_ric'] = ($objSess->ric_ope=='ope')?'':'S';
$aDefVarSess['f_tes_ope'] = ($objSess->ric_ope=='ope')?'S':'S';
$aDefVarSess['f_tes_chi'] = ($objSess->ric_ope=='ope')?'':'S';
$aDefVarSess['f_rint'] = '';
$aDefVarSess['f_tipo_rint'] = '';
$aDefVarSess['f_oggetto'] = '';
$aDefVarSess['f_rif_esterno'] = '';
$aDefVarSess['f_nome_sorg'] = '';
$aDefVarSess['f_solo_mie'] = ($objSess->ric_ope=='ope')?'S':'';
$aDefVarSess['f_comm'] = '';
$aDefVarSess['f_cli'] = '';
$aDefVarSess['f_sta'] = '';
foreach($aDefVarSess as $kX => $vX) {
	if (!isset($objSess->aSV['rintList'][$kX])) {
		$objSess->aSV['rintList'][$kX] = $vX;
	}
	if (reqVar('azione')=='') {
		$_REQUEST[$kX] = $objSess->aSV['rintList'][$kX];
	}
}
if (reqVar('azione')=='appfiltri') {
	foreach($aDefVarSess as $kX => $vX) {
		$objSess->aSV['rintList'][$kX] = reqVar($kX);
	}	
	$_REQUEST['startoffset'] = 0;
}
if (reqVar('azione')=='resfiltri') {
	foreach($aDefVarSess as $kX => $vX) {
		$objSess->aSV['rintList'][$kX] = $vX;
		$_REQUEST[$kX] = $vX;
	}	
	$_REQUEST['startoffset'] = 0;
}

$aFilt = $objSess->aSV['rintList'];
$sSql = "SELECT * FROM commessa ORDER BY IF(data_fine < '".date('Y-m-d')."',1,0),descr";
$objDB->query($sSql,'selVal');
$aComm = array(''=>'-- tutte --');
while($acX = $objDB->fetch('selVal')) {
	if ($acX['data_fine'] < date('Y-m-d')) {
		$aComm[$acX['id_comm']] = $acX['descr']." (chiusa)";
	} else {
		$aComm[$acX['id_comm']] = $acX['descr'];
	}
}
$aCliComm = array();
if ($aFilt['f_comm']!='') {
	$sSql = "SELECT id_cliente FROM clienti_comm WHERE id_comm = ".intval($aFilt['f_comm']);
	$objDB->query($sSql,'selVal');
	while($acX = $objDB->fetch('selVal')) {
		$aCliComm[] = $acX['id_cliente'];
	}
}	
$aCli = array(''=>'-- tutti --');
foreach($aClientiRint as $kX => $aX) {
	if ($aFilt['f_comm']!='' && !in_array($kX,$aCliComm)) {
		continue;
	}
	$aCli[$kX] = $aX['nome'];
}
if (!isset($aCli[$aFilt['f_cli']])) {
	$objSess->aSV['rintList']['f_cli'] = '';
	$aFilt = $objSess->aSV['rintList'];
}
$aSta = array(''=>'-- tutti --');
foreach($aStatiRint as $kX => $aX) {
	if ($kX==0) continue;
	$aSta[$kX] = $aX['descr'];
}
$aTipoR = array(''=>'-- tutti --');
foreach($aTipiRint as $kX => $aX) {
	if ($kX==0) continue;
	$aTipoR[$kX] = $aX['des'];
}

$aWhere = array();
if ($aFilt['f_rint'] != 0) { //se imposta numero rint disattende gli altri filtri
	$aWhere[] = " r.id_rint = ".intval($aFilt['f_rint'])." ";
} else {
	if ($aFilt['f_solo_mie'] == 'S') {
		if ($objSess->ric_ope=='ric') {
			$aWhere[] = " r.id_richiedente = ".$objSess->idUtente." ";
		} else {
			$aWhere[] = " (r.id_responsabile = ".$objSess->idUtente." OR "
				." (".$objSess->idUtente." IN (SELECT za.id_operatore FROM assegnaz za WHERE za.id_rint = r.id_rint))) ";
		}
	}
	if ($aFilt['f_oggetto']!='') {
		$aWhere[] = " (r.descr LIKE '%".addslashes($aFilt['f_oggetto'])."%') ";
	}
	if ($aFilt['f_rif_esterno']!='') {
		$aWhere[] = " (r.rif_esterno LIKE '%".addslashes($aFilt['f_rif_esterno'])."%') ";
	}
	if ($aFilt['f_nome_sorg']!='') {
		$aWhere[] = " (o.nome_sorg LIKE '%".addslashes($aFilt['f_nome_sorg'])."%') ";
	}
	if ($aFilt['f_comm']!='') {
		$aWhere[] = " (r.id_comm = ".intval($aFilt['f_comm']).") ";
	}
	if ($aFilt['f_cli']!='') {
		$aWhere[] = " (r.id_cliente = ".intval($aFilt['f_cli']).") ";
	}
	if ($aFilt['f_sta']!='') {
		$aWhere[] = " (r.stato = ".intval($aFilt['f_sta']).") ";
	}
	if ($aFilt['f_tipo_rint']!='') {
		$aWhere[] = " (r.tipo_rint = ".intval($aFilt['f_tipo_rint']).") ";
	}
	if (($aFilt['f_tes_ric'].$aFilt['f_tes_ope'].$aFilt['f_tes_chi'])!='SSS') {
		$aStX = array(0);
		foreach($aStatiRint as $kX => $aX) {
			if (($aX['testimone']=='ric' && $aFilt['f_tes_ric']=='S')
			|| ($aX['testimone']=='ope' && $aFilt['f_tes_ope']=='S')
			|| ($aX['testimone']=='chiuso' && $aFilt['f_tes_chi']=='S')) {
				$aStX[] = $kX;
			}
		}
		$aWhere[] = " r.stato IN (".implode(",",$aStX).") ";
	}
}

$sWhere = (count($aWhere)==0)? "":" WHERE ".implode(" AND ",$aWhere);
//dbg_($sWhere);
$sql_sel  = "SELECT DISTINCT r.id_rint FROM rint r ";
$sql_sel .= " LEFT JOIN rint_sorg_mod o ON o.id_rint = r.id_rint ";
$sql_sel .= $sWhere;	 
$objDB->query($sql_sel,'selRec');
$numRec = $objDB->numRec('selRec');
$link = $_SERVER['PHP_SELF']."?".$sXSNQS;
$startOffset = 0;
$endOffset = 0;
$sFiltro = rint2GetFiltro($link);
$sPaginaz = rint2GetPaginaz($link, $numRec, $startOffset, $endOffset);

$sql_sel  = "SELECT DISTINCT r.id_rint FROM rint r ";
$sql_sel .= " LEFT JOIN rint_sorg_mod o ON o.id_rint = r.id_rint ";
$sql_sel .= $sWhere;	 
$sql_sel .= " ORDER BY IF(r.stato >= 90,1,0), r.id_rint DESC ";
$sql_sel .= " LIMIT ".$startOffset.",".($endOffset-$startOffset);
$objDB->query($sql_sel,'selRec');
$aIdX = array(0);
while ($acX = $objDB->fetch('selRec')) {
	$aIdX[] = $acX['id_rint'];
}
$sql_sel =  " SELECT r.*, c.cod_comm, c.descr AS des_comm 
              	FROM rint r 
			  		INNER JOIN commessa c ON c.id_comm = r.id_comm ";
if ($aUtentiRint[$objSess->idUtente]['ric_ope'] == 'ric') {
	$sql_sel .= " INNER JOIN richiedenti ri ON ri.id_comm = c.id_comm ".
	            "                         AND ri.id_operatore = ".$objSess->idUtente;		
} 			 
$sql_sel .= " WHERE r.id_rint IN (".implode(",",$aIdX).") ";	 
$sql_sel .= " ORDER BY IF(r.stato >= 90,1,0), r.id_rint DESC ";
$objDB->query($sql_sel,'selRec');

$cmdL = "";
$cmdR = '';
$cmdR .= "&nbsp;<a href=\"javascript:submFilt('appfiltri');\" class='cmd1'>applica filtri</a>";
$cmdR .= "&nbsp;&nbsp;&nbsp;";
$cmdR .= "&nbsp;<a href=\"javascript:submFilt('resfiltri');\" class='cmd1'>annulla filtri</a>";
if ($objSess->ric_ope=='ric' || rint2Vx($objSess->abilitaz,'richied')=='S') {
	$cmdL .= "<a class=\"cmd1\" href=\"rintIns.php?$sXSNQS\" style='color:red;'>nuova richiesta intervento</a>";
	$cmdL .= "&nbsp;&nbsp;&nbsp;";
}
if ($objSess->ric_ope=='ope') {
	$cmdL .= "<a class=\"cmd1\" href=\"rintSpot.php?$sXSNQS\" style='color:red;'>richiesta SPOT</a>";
	$cmdL .= "&nbsp;&nbsp;&nbsp;";
}
$cmdR .= cmdTorna(); 
	
$aMenuPag = array();
include("_inc/inc.aMenuPag.php");
testaHtml('Lista richieste intervento', $msgPag, '30.10.10', $aMenuPag);
?>
<script type="text/javascript">
<!--
function submFilt(az) {
	document.frm1.azione.value = az;
	document.frm1.startoffset.value = '0';
	document.frm1.submit();
}
//-->
</script>
<?php 
echo getHtmlForm();
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('startoffset');
echo getHtmlHidden('azione','...');
?>
<table class="cmd1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="lab1" align="center" colspan='3'>
      		Filtri
      </td>
   </tr>
   <tr>
      <td class="txt1" align="left">
			<?
			$aOpt1 = array("onClick"=>"submFilt('appfiltri');");
			$aOpt2 = array("onChange"=>"submFilt('appfiltri');");
			echo "Testimone:&nbsp;";
			?>
		</td>
		<td class="txt1" align="left">
			<?php 
			echo getHtmlCheckBox('f_tes_ric','S','@REQ',$aOpt1)
				."&nbsp;<img src='".R2_URL_ROOT."/app/_img/pallino_verde.gif' />&nbsp;&nbsp;&nbsp;"
				."richiedente&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
			echo getHtmlCheckBox('f_tes_ope','S','@REQ',$aOpt1)
				."&nbsp;<img src='".R2_URL_ROOT."/app/_img/pallino_rosso.gif' />"
				."&nbsp;<img src='".R2_URL_ROOT."/app/_img/pallino_giallo.gif' />&nbsp;&nbsp;&nbsp;"
				."operatore&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
			echo getHtmlCheckBox('f_tes_chi','S','@REQ',$aOpt1)
				."&nbsp;<img src='".R2_URL_ROOT."/app/_img/pallino_bianco.gif' />&nbsp;&nbsp;&nbsp;"
				."chiuso&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
			?>
      </td>
      <td class="txt1" align="right">
			<?
			echo getHtmlCheckBox('f_solo_mie','S','@REQ',$aOpt1)."&nbsp;Solo le mie&nbsp;&nbsp;&nbsp;";
			echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
			echo "N.rint:&nbsp;".getHtmlText('f_rint',10);
			?>
      </td>
   </tr>
   <tr>
      <td class="txt1" align="left">
			<?
			echo "Commessa:&nbsp;";
			?>
		</td>
		<td class="txt1" align="left" colspan='2'>
			<?php 			
			echo getHtmlSelect('f_comm',$aComm,'@REQ',$aOpt2);
			echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
			echo "Cliente:&nbsp;&nbsp;";
			echo getHtmlSelect('f_cli',$aCli,'@REQ',$aOpt2);
			echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
			echo "Stato:&nbsp;&nbsp;";
			echo getHtmlSelect('f_sta',$aSta,'@REQ',$aOpt2);
			echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
			echo "Tipo rich.:&nbsp;&nbsp;";
			echo getHtmlSelect('f_tipo_rint',$aTipoR,'@REQ',$aOpt2);
			?>
      </td>
   </tr>
   <tr>
      <td class="txt1" align="left">
			<?
			echo "Oggetto:&nbsp;";
			?>
		</td>
		<td class="txt1" align="left" colspan='2'>
			<?php 
			echo getHtmlText('f_oggetto',40);
			echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
			echo "Riferimento:&nbsp;&nbsp;";
			echo getHtmlText('f_rif_esterno',40);
			echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
			echo "Ogg.mod.:&nbsp;&nbsp;";
			echo getHtmlText('f_nome_sorg',40);
			?>
      </td>
   </tr>
</table>
<table class="cmd1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
      	&nbsp;
      </td>
      <td class="cmd1" align="right">
      		<?=$cmdR?>
      </td>
   </tr>
   <tr>
      <td class="cmd1" align="left">
			<?=$cmdL?>&nbsp;
      </td>
      <td class="cmd1" align="right">
			<?
			echo $sPaginaz;
			?>
      </td>
   </tr>
</table>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="tit1" colspan='10' align='center'>Lista rint</td>
   </tr>
   <tr>
      <td class="lab1" width='' align='center' width='1%'>N.</td>
      <td class="lab1" width='' align='center'>Data apertura<br/>Data modifica</td>
      <td class="lab1" width='' align='center'>Commessa<br/>Oggetto</td>
      <td class="lab1" width='' align='center'>Cliente<br/>Richiedente</td>
      <td class="lab1" width='' align='center' >Stato<br/>priorita</td>
      <td class="lab1" width='' align='center' >Tipo rich.<br/>Valutata/Evasa</td>
      <td class="lab1" width='' align='center' >Scad.valutaz.<br/>Scad.evasione</td>
      <td class="lab1" width='' align='center'>Ore<br/>val.</td>
      <td class="lab1" width='' align='center'>Ore<br/>lav.</td>
   </tr>
<? 
$clRF = 'txt2';
while($acR = $objDB->fetch('selRec')) { 
	$clRF = ($clRF=='txt1')?'txt2':'txt1';	
	$oreLav = $oreStima = 0;
	$sSql = "SELECT SUM(ore) AS tot_ore FROM attivita WHERE id_rint = ".$acR['id_rint']." GROUP BY id_rint";
	$objDB->query($sSql);
	$acX = $objDB->fetch();
	$oreLav = $acX['tot_ore'];
	$sSql = "SELECT SUM(stima) AS tot_ore FROM att_previste WHERE id_rint = ".$acR['id_rint']." GROUP BY id_rint";
	$objDB->query($sSql);
	$acX = $objDB->fetch();
	$oreStima = $acX['tot_ore'];	
	$sSql = "SELECT id_operatore FROM assegnaz WHERE id_rint = ".$acR['id_rint'];
	$objDB->query($sSql);
	$bAss = $bMio = false;
	while($acX = $objDB->fetch()) {
		$bAss = true;
		if ($acX['id_operatore'] == $objSess->idUtente) {
			$bMio = true;
		}
	}	
	$sHrefRint = "rintView.php?id=".$acR['id_rint']."&$sXSNQS";
	$sValEva = "";
	if ($acR['evasa'] == 'S') {
		$sValEva = $aFlagEva[$acR['evasa']];	
	} elseif ($acR['valutata'] == 'S') {
		$sValEva = $aFlagVal[$acR['valutata']];
	} elseif ($acR['evasa'] == 'N') {
		$sValEva = $aFlagEva[$acR['evasa']];
	} elseif ($acR['valutata'] == 'N') {
		$sValVal = $aFlagVal[$acR['valutata']];
	} elseif ($acR['evasa'] == 'C') {
		$sValEva = $aFlagEva[$acR['evasa']];
	} 
	if (isset($aTipiRint[$acR['tipo_rint']])) { 
		$sTipoRint = $aTipiRint[$acR['tipo_rint']]['desBreve']; 
  } else {
    	$sTipoRint = $acR['tipo_rint']; ;		
  } 
?>
   <tr>
      <td class="<?=$clRF?>" style="text-align:left;font-size:12pt;" valign='top'>
      	<img src='<?= R2_URL_ROOT?>/app/_img/pallino_<?=$aStatiRint[$acR['stato']]['pallino']?>.gif'
      		onClick="document.location.href='<?=$sHrefRint?>';"
      		style='cursor:pointer;' />
      	<a href='<?=$sHrefRint?>' class="<?=$clRF?>" style="font-size:12pt;font-weight:bold;">
            <?=$acR['id_rint'];?>
         </a><br/>
         <img src='<?= R2_URL_ROOT?>/app/_img/barra<?=round($acR['stato']/25);?>.gif' width='80' height='8'
      		onClick="document.location.href='<?=$sHrefRint?>';"
      		style='cursor:pointer;border:black 1px solid;' />
      </td>
      <td class="<?=$clRF?>" align="center" valign='top'>
      	<a href='<?=$sHrefRint?>' class="<?=$clRF?>">
      		<?=YMD2DMY($acR['data_ora_ins']);?>
      		<br/>
      		<?=YMD2DMY($acR['data_ora_mod']);?>
      	</a>    	
      </td>
      <td class="<?=$clRF?>" align="left" valign='top'>
      	<a href='<?=$sHrefRint?>' class="<?=$clRF?>">
      	<?=substr($acR['des_comm'],0,50);?><br/><?=wordwrap($acR['descr'],50,"<br/>",true);?>      	
	      </a>
      </td>
      <td class="<?=$clRF?>" align="left" valign='top'>
	     <? if (isset($aClientiRint[$acR['id_cliente']])) {
	      		echo $aClientiRint[$acR['id_cliente']]['nome']; 
	      	} else  {
	      		echo "???&nbsp;";
	      	} ?><br/>
	      <?=rint2Vx($aUtentiRint,$acR['id_richiedente'],'nome');?>	    	
      </td>
      <td class="<?=$clRF?>" align="left" valign='top'>
      	<?
      	echo $acR['stato'];?> - <?=$aStatiRint[$acR['stato']]['descr'];
      	$sStyle='';
      	if ($acR['priorita']>=4) {
      		$sStyle .= "color:red;";
      	}
      	if ($acR['priorita']>=5) {
      		$sStyle .= "font-weight:bold;";
      	}
      	echo "<div style='text-align:center;$sStyle'>Priorita' ".$aPrioritaRint[$acR['priorita']]['des']."</div>";
      	?>
      </td>
      <td class="<?=$clRF?>" style="text-align:center;" valign='top'>
      	<?echo $sTipoRint."<br/>".$sValEva;?>
      </td>
      <td class="<?=$clRF?>" style="text-align:center;" valign='top'>
      	<?=YMD2DMY($acR['data_scad_valutaz'])?>
      	<br/>
      	<?=YMD2DMY($acR['data_scad_svil'])?>
      </td>
      <td class="<?=$clRF?>" style="text-align:right;" valign='top'>
      	<i><?=($oreStima!=0)?number_format($oreStima,1,',','.'):"";?>&nbsp;</i>
      	<?
      	if ($objSess->ric_ope=='ope') {
      		echo "<br/>";
	      	if ($bMio) { 
	      		echo "mio"; 
	      	} 	elseif ($bAss) { 
	      		echo "altri"; 
	      	} else { 
	      			echo "<b>ass.?</b>"; 
	      	}
      	}
      	?>
      	
      </td>
      <?
      if ($oreStima > 0 && $oreLav > $oreStima) {
      	$sCol = "color:red;";
      } else {
      	$sCol = '';
      }
      ?>
      <td class="<?=$clRF?>" style="text-align:right;<?=$sCol?>" valign='top'>
      	<?=($oreLav!=0)?number_format($oreLav,1,',','.'):"";?>&nbsp;
      </td>
   </tr>
<? } ?>
</table>
<table class="cmd1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left">
         <?=$cmdL?>&nbsp;
      </td>
      <td class="cmd1" align="right">
			<?=$sPaginaz?>
      </td>
   </tr>
   <tr>
      <td class="cmd1" align="left">
         &nbsp;
      </td>
      <td class="cmd1" align="right">
         <?=$cmdR?>
      </td>
   </tr>
</table>
<?
echo "</form>";

codaHtml();
exit();
?>
